package com.hqbzl.chat.handler;

import java.util.Objects;
import java.util.Set;

import com.hqbzl.chat.factory.Factory;
import com.hqbzl.chat.model.GSendMessage;
import com.hqbzl.chat.model.SendMessage;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

@Sharable
public final class GrupSendMessageHandler extends SimpleChannelInboundHandler<GSendMessage> {

	// 群内发消息
	@Override
	protected void channelRead0(ChannelHandlerContext ctx, GSendMessage msg) throws Exception {
		String userName = msg.getUserName();
		String groupName = msg.getGroupName();
		String context = msg.getContext();
		Set<String> members = Factory.groupSession.getMembers(groupName);
		members.forEach(x -> {
			Channel channel = Factory.session.getChannel(x);
			if (channel != null) {
				SendMessage build = SendMessage.builder().userName(userName).targetUser(x).context(context).build();
				channel.writeAndFlush(build);
			}
		});

	}
}